package com.ych.rabbitmq.listener;

import com.ych.rabbitmq.exchange.DemoExchange;
import com.ych.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/11
 * Time: 11:47
 * Description:
 */
@Component
@RabbitListener(queues = DemoExchange.QUEUE_NAME)
@Slf4j
public class DemoListener {
    //发送延迟队列方法，延迟15s
    //rabbitTemplate.convertAndSend(RefundAndRedPacketExchange.EXCHANGE_NAME, RefundAndRedPacketExchange.QUEUE_NAME, new MapUtils().put("type", "redPacket").put("orderId", rechangeOrder),
    //    message -> {
    //        message.getMessageProperties().setHeader("x-delay", 15 * 1000);//15秒
    //        return message;
    //    });
    /**
     * 延时订单
     *
     * @param out_trade_no 订单号
     */
    @RabbitHandler
    public void orderDelayConsumer(String out_trade_no) {
        System.out.println("订单号："+out_trade_no);
        for (int i = 0; i < 10; i++) {
            System.out.println(DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN));
            System.out.println("MQ调度："+i);
        }
        //消费者接收到以后的业务代码
//        String lockName = "Lock_Grant_Vip_Order_" + out_trade_no;
//        String lockId = UUID.randomUUID();
//        if (RedisLockUtil.tryLock(lockName, lockId, 0, 300)) {
//            try {
//                iOrderInfoService.yopPayOrderNotify(out_trade_no);
//            } catch (Exception e) {
//                log.error("延时赠送VIP订单错误信息{},订单号:{}", e.getMessage(), out_trade_no);
//            } finally {
//                RedisLockUtil.unlock(lockName, lockId);
//            }
//        }

    }
}
